﻿package com.designoutput.ui.newControls
{
	import com.designoutput.icon.BoxTopLeft;
	import com.designoutput.icon.BoxTopMiddle;
	import com.designoutput.icon.BoxTopRight;
	import com.designoutput.icon.CloseOn;
	import com.designoutput.icon.FloatBoxBg;
	import com.designoutput.icon.UI;
	import com.designoutput.ui.controls.IconButton;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.text.*;

	public class Box extends ControlBase
	{
		private var tr:Bitmap;
		private var bm:Bitmap;
		private var bl:Bitmap;
		private var br:Bitmap;
		private var ml:Bitmap;
		private var mr:Bitmap;
		private var mm:Bitmap;
		private var tl:Bitmap;
		private var tm:Bitmap;
		
		private var closeButton:IconButton;
		private var drag:Sprite;
		private var titleTextField:TextField;
		private var _showCloseButton:Boolean = false;
		
		override protected function initialize():void {
			this.filters = [new DropShadowFilter(0, 45, 0, 0.4, 10, 10, 1, 3, false)];
			this.tl=new Bitmap(new BoxTopLeft(0, 0));
			addChild(this.tl);
			this.tm=new Bitmap(new BoxTopMiddle(0, 0));
			addChild(this.tm);
			this.tr=new Bitmap(new BoxTopRight(0, 0));
			addChild(this.tr);
			var _floatBox:*=new FloatBoxBg(0, 0);
			this.ml=new Bitmap(UI.GetPart(_floatBox, 0, 102, 32, 10));
			addChild(this.ml);
			this.mm=new Bitmap(UI.GetPart(_floatBox, 32, 102, 10, 10));
			addChild(this.mm);
			this.mr=new Bitmap(UI.GetPart(_floatBox, 42, 102, 32, 10));
			addChild(this.mr);
			this.bl=new Bitmap(UI.GetPart(_floatBox, 0, 112, 32, 10));
			addChild(this.bl);
			this.bm=new Bitmap(UI.GetPart(_floatBox, 32, 112, 10, 10));
			addChild(this.bm);
			this.br=new Bitmap(UI.GetPart(_floatBox, 42, 112, 32, 10));
			addChild(this.br);
			_floatBox.dispose();
			_floatBox=null;
			this.titleTextField=new TextField();
			this.titleTextField.defaultTextFormat=new TextFormat("Arial,Helvetica,San-serif", 12, 16777215, false, false, false, null, null, "center");
			this.titleTextField.height=18;
			this.titleTextField.multiline=false;
			this.titleTextField.selectable=false;
			addChild(this.titleTextField);
			this.drag=new Sprite();
			this.drag.graphics.beginFill(9474192);
			this.drag.graphics.drawRect(0, 0, 100, 100);
			this.drag.graphics.endFill();
			this.drag.alpha=0;
			this.drag.addEventListener(MouseEvent.MOUSE_DOWN, this.dragMouseDown, false, 0, true);
			this.drag.addEventListener(MouseEvent.MOUSE_UP, this.dragMouseUp, false, 0, true);
			addChild(this.drag);
		}
		
		public function get showCloseButton():Boolean {
			return _showCloseButton;
		}
		
		public function set showCloseButton(value:Boolean):void {
			_showCloseButton = value;
			invalidateDisplay();
		}

		override protected function get minWidth():Number {
			return 64;
		}
		
		override protected function get minHeight():Number {
			return 115;
		}
		
		override protected function updateDisplay():void {
			super.updateDisplay();
			
			tm.width = mm.width = bm.width = _width - 64;
			ml.height = mm.height = mr.height = _height - 115;
			tm.x = mm.x = bm.x = 32;
			tr.x = mr.x = br.x = tm.width + 32;
			ml.y = mm.y = mr.y = 100;
			br.y = bm.y = bl.y = mm.height + 100;
			titleTextField.x = 0;
			titleTextField.width = _width;
			drag.x = drag.y = 1;
			drag.width = _width - 2;
			drag.height = 17;
			
			if(_showCloseButton){
				if(closeButton == null){
					closeButton = new IconButton(new CloseOn(0, 0));
					closeButton.addEventListener(MouseEvent.MOUSE_UP, cancelClick, false, 0, true);
					addChild(closeButton);
				}
				
				closeButton.y = 4;
				closeButton.x = _width - 17;
			} else if(closeButton != null){
				closeButton.removeEventListener(MouseEvent.MOUSE_UP, cancelClick, false);
				removeChild(closeButton);
				closeButton = null;
			}
		}

		protected function dragMouseUp(event:MouseEvent):void
		{
			this.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, this.dragMouseUp, true);
		} 

		public function setTitle(param1:String):void
		{
			this.titleTextField.text=param1;
		} 

		internal function dragMouseDown(event:MouseEvent):void
		{
			this.startDrag(false, new Rectangle((-this.width) / 2, 60, stage.stageWidth, stage.stageHeight + 1000));
			stage.addEventListener(MouseEvent.MOUSE_UP, this.dragMouseUp, true, 0, true);
		} 

		private function cancelClick(event:MouseEvent):void
		{
			onCloseClick();
		}
		
		protected function onCloseClick():void {
			
		}
	}
}
